package com.rd.student.jiaji.class1.wangyufang.day0803;

import java.util.ArrayList;

class D implements Comparable {
    int age;

    public D(int age) {
        this.age = age;
    }
    @Override
    public int compareTo(Object o) {
        D d = (D) o;
        if (this.age > d.age) {
            return -1;
        } else if (this.age < d.age) {
            return 1;
        }
        return 0;
    }

    @Override
    public String toString() {
        return "D{" +
                "age=" + age +
                '}';
    }
    public static void main(String[] args){
        ArrayList<D> arrayList = new ArrayList<>();
        D d = new D(15);
        D d1 = new D(50);
        D d2 = new D(25);
        D d3 = new D(18);
        arrayList.add(d);
        arrayList.add(d1);
        arrayList.add(d2);
        arrayList.add(d3);
        System.out.println(arrayList);
        for (int i = 0; i < arrayList.size()- 1; i++) {
            for (int j = 0; j < arrayList.size()- i - 1; j++) {
                if (arrayList.get(j).compareTo(arrayList.get(j + 1)) < 0) {
                    D temp = arrayList.get(j);
                    arrayList.set(j,arrayList.get(j+1));
                    arrayList.set(j+1,temp);
                    System.out.println(arrayList);
                }
            }
        }
        System.out.println("最终结果为："+arrayList);
    }
}

